package com.excilys.training.ebanking.web.java;


import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.format.Alignment;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.excilys.training.ebanking.model.Operation;
import org.springframework.web.servlet.view.document.AbstractJExcelView;

public class ExcelGenerator extends AbstractJExcelView {
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void buildExcelDocument(Map model,
			WritableWorkbook wb, HttpServletRequest arg2,
			HttpServletResponse arg3) throws Exception {
		
		int id =(Integer)model.get("numeroCompte"); 
		String mois = (String)model.get("nomMois");
		int year = (Integer)model.get("year");
		List<Operation> operations =(List<Operation>)model.get("operationsByMonth");
		
		int debutcolonne=1;
		int debutligne=1;
		
		WritableFont fontTitre = new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD);
		WritableFont fontType = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD);
		WritableFont fontLibelle = new WritableFont(WritableFont.ARIAL);
		WritableCellFormat cellFormatTitre = new WritableCellFormat(fontTitre);
		WritableCellFormat cellFormatType = new WritableCellFormat(fontType);
		WritableCellFormat cellFormatLibelle = new WritableCellFormat(fontLibelle);
		
		cellFormatTitre.setWrap(true);
		cellFormatLibelle.setWrap(true);
		cellFormatTitre.setAlignment(Alignment.CENTRE);
		cellFormatLibelle.setAlignment(Alignment.CENTRE);
		cellFormatType.setAlignment(Alignment.CENTRE);
		
		WritableSheet sheet = wb.createSheet("compte n°"+id, 0);
		
		sheet.addCell( new Label(debutcolonne, debutligne, " RELEVE COMPTE n° "+id+" DU : "+mois+"   "+year,cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+1, debutligne, " COMPTE",cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+2, debutligne, " "+id,cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+3, debutligne, " RELEVE DU COMPTE"+mois,cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+4, debutligne, " RELEVE DU COMPTE"+mois,cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+5, debutligne, " RELEVE DU COMPTE"+mois,cellFormatTitre));
		sheet.addCell( new Label(debutcolonne+6, debutligne, " RELEVE DU COMPTE"+mois,cellFormatTitre));

		

		
		sheet.mergeCells(debutcolonne, debutligne, debutcolonne+6, debutligne+1);
		sheet.addCell(new Label(debutcolonne+1,debutligne+3," Date ",cellFormatType));
		sheet.addCell(new Label(debutcolonne+2,debutligne+3,""));
		sheet.mergeCells(debutcolonne+1, debutligne+3, debutcolonne+2, debutligne+3);

		sheet.addCell(new Label(debutcolonne+3,debutligne+3," Libelle ",cellFormatType));
		sheet.addCell(new Label(debutcolonne+4,debutligne+3,""));
		sheet.mergeCells(debutcolonne+3, debutligne+3, debutcolonne+4, debutligne+3);
		sheet.addCell(new Label(debutcolonne+5, debutligne+3," Montant(euros)", cellFormatType));
		sheet.addCell(new Label(debutcolonne+6, debutligne+3," Montant (euros) ", cellFormatType));
		sheet.mergeCells(debutcolonne+5, debutligne+3, debutcolonne+6, debutligne+3);
		DateFormat formatDate = new DateFormat ("dd MMM yyyy hh:mm:ss"); 

		//NumberFormat formatNombre = new NumberFormat("#.##");
		
		for(int i=0;i<operations.size();i++){
			
			WritableCellFormat cellFormatDate = new WritableCellFormat (formatDate); 
			cellFormatDate.setWrap(true);
			
			Date date = operations.get(i).getDateOperation().toDate();
			DateTime dateCell = new DateTime(debutcolonne+1,debutligne+5+i, date, cellFormatDate);
			sheet.addCell(dateCell);
			sheet.addCell(new Label(debutcolonne+2,debutligne+5+i,""));
			sheet.mergeCells(debutcolonne+1, debutligne+5+i, debutcolonne+2, debutligne+5+i);
			
			sheet.addCell( new Label(debutcolonne+3,debutligne+5+i, operations.get(i).getLibelle(),cellFormatLibelle));
			sheet.addCell(new Label(debutcolonne+4,debutligne+5+i,""));
			sheet.mergeCells(debutcolonne+3, debutligne+5+i, debutcolonne+4, debutligne+5+i);

			sheet.addCell(new Number(debutcolonne+5,debutligne+5+i, operations.get(i).getMontant()));
		}
	}

}
